package com.authine.cloudpivot.web.api.controller;

import com.alibaba.fastjson.JSONObject;
import com.authine.cloudpivot.web.api.exception.PortalException;
import com.authine.cloudpivot.web.api.handler.CustomizedOrigin;
import com.authine.cloudpivot.web.api.service.AsyncDataService;
import com.authine.cloudpivot.web.api.service.ExecuteService;
import com.authine.cloudpivot.web.api.view.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author zhangjie
 * @date 2019-12-09
 */
@Api(value = "定制的查询列表数据接口", tags = "招聘需求")
@RestController
@RequestMapping("/api/execute")
@Slf4j
@CustomizedOrigin(level = 0)
public class ExecuteController {
    @Autowired
    private ExecuteService executeService;

    private AsyncDataService asyncDataService;

    ExecutorService executorService = Executors.newFixedThreadPool(2);

    @ApiOperation(value = "创建执行周报")
    @PostMapping("/createExecuteReport")
    public ResponseResult<?> createWeeklyReport(@RequestParam("id") String id) {
        JSONObject json = new JSONObject();
        Long statusReal = 200L;
        try {
            executeService.createExecuteReport(id);
            json.put("message","创建周报成功");
        }catch (PortalException e) {
            statusReal = 500L;
            json.put("message", "创建周报失败：" + e.getErrMsg());
        }
        return ResponseResult.<JSONObject>builder().errcode(statusReal).data(json).build();
    }

    @ApiOperation(value = "更新rcs区域")
    @PostMapping("/updateRCSArea")
    public ResponseResult<?> updateRCSArea(@RequestParam("id") String id, @RequestParam("schemaCode") String schemaCode) {
        JSONObject json = new JSONObject();
        Long statusReal = 200L;
        try {
            executeService.updateRCSArea(id,schemaCode);
            json.put("message","更新rcs区域成功");
        }catch (PortalException e) {
            statusReal = 500L;
            json.put("message", "更新rcs区域失败：" + e.getErrMsg());
        }
        return ResponseResult.<JSONObject>builder().errcode(statusReal).data(json).build();
    }

    @ApiOperation(value = "更新执行需求")
    @PostMapping("/updateExecuteDemand")
    public ResponseResult<?> updateExecuteDemand(@RequestParam("id") String id) {
        JSONObject json = new JSONObject();
        Long statusReal = 200L;

        try {

            executorService.execute(()->{
                long l = System.currentTimeMillis();
                log.info("\n------------进入更新执行需求");
                executeService.updateExecuteDemand(id);
                log.info("\n----更新执行需求结束:{}",System.currentTimeMillis()-l);
            });

            json.put("message","更新执行需求成功");
        }catch (PortalException e) {
            statusReal = 500L;
            json.put("message", "更新执行需求失败：" + e.getErrMsg());
        }
        return ResponseResult.<JSONObject>builder().errcode(statusReal).data(json).build();
    }

    @ApiOperation(value = "关闭执行需求")
    @PostMapping("/closeExecuteDemand")
    public ResponseResult<?> closeExecuteDemand(@RequestParam("id") String id) {
        JSONObject json = new JSONObject();
        Long statusReal = 200L;
        try {
            executeService.closeExecuteDemand(id);
            json.put("message","关闭执行需求成功");
        }catch (PortalException e) {
            statusReal = 500L;
            json.put("message", "关闭执行需求失败：" + e.getErrMsg());
        }
        return ResponseResult.<JSONObject>builder().errcode(statusReal).data(json).build();
    }

    @ApiOperation(value = "更新成员")
    @PostMapping("/updateMember")
    public ResponseResult<?> updateMember(@RequestParam("id") String id, @RequestParam("projectId") String projectId, @RequestParam("role")String role, @RequestParam("phone")String phone, @RequestParam("way")String way) {
        JSONObject json = new JSONObject();
        Long statusReal = 200L;
        try {
            executeService.updateMember(id, projectId,role, phone, way);
            json.put("message","更新成员成功");
        }catch (PortalException e) {
            statusReal = 500L;
            json.put("message", "更新成员失败：" + e.getErrMsg());
        }
        return ResponseResult.<JSONObject>builder().errcode(statusReal).data(json).build();
    }

    @ApiOperation(value = "新增成员")
    @PostMapping("/addMember")
    public ResponseResult<?> addMember(@RequestParam("id") String id, @RequestParam("schemaCode") String schemaCode) {
        JSONObject json = new JSONObject();
        Long statusReal = 200L;
        try {
            executeService.addMember(id,schemaCode);
            json.put("message","新增成员成功");
        }catch (PortalException e) {
            statusReal = 500L;
            json.put("message", "新增成员失败：" + e.getErrMsg());
        }
        return ResponseResult.<JSONObject>builder().errcode(statusReal).data(json).build();
    }

    @ApiOperation(value = "更新项目经理")
    @PostMapping("/updatePM")
    public ResponseResult<?> updatePM(@RequestParam("id") String id) {
        JSONObject json = new JSONObject();
        Long statusReal = 200L;
        try {
            executeService.updatePM(id);
            json.put("message","更新项目经理成功");
        }catch (PortalException e) {
            statusReal = 500L;
            json.put("message", "更新项目经理失败：" + e.getErrMsg());
        }
        return ResponseResult.<JSONObject>builder().errcode(statusReal).data(json).build();
    }

    @ApiOperation(value = "更新执行经理")
    @PostMapping("/updateEM")
    public ResponseResult<?> updateEM(@RequestParam("id") String id) {
        JSONObject json = new JSONObject();
        Long statusReal = 200L;
        try {
            executeService.updateEM(id);
            json.put("message","更新执行经理成功");
        }catch (PortalException e) {
            statusReal = 500L;
            json.put("message", "更新执行经理失败：" + e.getErrMsg());
        }
        return ResponseResult.<JSONObject>builder().errcode(statusReal).data(json).build();
    }

    @ApiOperation(value = "更新配置文件")
    @PostMapping("/updateSystemConfig")
    public ResponseResult<?> updateSystemConfig(){
        JSONObject json = new JSONObject();
        Long statusReal = 200L;
        try {
            executeService.updateSystemConfig();
            json.put("message","更新配置文件成功");
        }catch (PortalException e) {
            statusReal = 500L;
            json.put("message", "更新配置文件失败：" + e.getErrMsg());
        }
        return ResponseResult.<JSONObject>builder().errcode(statusReal).data(json).build();
    }

    @ApiOperation(value = "判断当前项目下的所有需求是否已关闭")
    @PostMapping("/judgeAllDemandClosed")
    public ResponseResult<?> judgeAllDemandClosed(@RequestParam("projectName") String projectName){
        JSONObject json = new JSONObject();
        Long statusReal = 200L;
        boolean flag = false;
        try {
            flag=  executeService.judgeAllDemandClosed(projectName);
            json.put("flag",flag);
            if(flag) {
                json.put("message", "当前项目下仍存在执行中和审核中的需求！");
            }else{
                json.put("message", "当前项目下已经不存在执行中和审核中需求！");
            }
        }catch (PortalException e) {
            statusReal = 500L;
            json.put("flag",flag);
            json.put("message", "调用接口失败：" + e.getErrMsg());
        }
        return ResponseResult.<JSONObject>builder().errcode(statusReal).data(json).build();
    }


    @ApiOperation(value = "项目延期")
    @PostMapping("/projectDelay")
    public ResponseResult<?> projectDelay(@RequestParam("projectId") String projectId){
        JSONObject json = new JSONObject();
        Long statusReal = 200L;
        try {
            executeService.projectDelay(projectId);
            json.put("message","项目延期成功");
        }catch (PortalException e) {
            statusReal = 500L;
            json.put("message", "项目延期失败：" + e.getErrMsg());
        }
        return ResponseResult.<JSONObject>builder().errcode(statusReal).data(json).build();
    }

    @ApiOperation(value = "更新门店")
    @PostMapping("/updateStoreState")
    public ResponseResult<?> updateStoreState(@RequestParam("id") String id, @RequestParam("storeShortName") String storeShortName, @RequestParam("rcsArea") String rcsArea,
                                              @RequestParam("province") String province, @RequestParam("cityName") String cityName, @RequestParam("district") String district,
                                              @RequestParam("detailArea") String detailArea, @RequestParam("area") String area, @RequestParam("storeOutlet") String storeOutlet,
                                              @RequestParam("customDockingPerson") String customDockingPerson, @RequestParam("customLinkType") String customLinkType,
                                              @RequestParam("cityLevel") String cityLevel,@RequestParam("state") String state){
        JSONObject json = new JSONObject();
        Long statusReal = 200L;
        try {
            executeService.updateStoreState(id,storeShortName,rcsArea,province,cityName,district,detailArea,area,storeOutlet,customDockingPerson,customLinkType,cityLevel,state);
            json.put("message","更新门店信息成功");
        }catch (PortalException e) {
            statusReal = 500L;
            json.put("message", "更新门店信息失败：" + e.getErrMsg());
        }
        return ResponseResult.<JSONObject>builder().errcode(statusReal).data(json).build();
    }


    @ApiOperation(value = "手动生成周报")
    @PostMapping("/HMCreateReport")
    public ResponseResult<?> HMCreateReport(@RequestParam("demandId") String demandId, @RequestParam("schemaCode") String schemaCode, @RequestParam("year") String year,
                                            @RequestParam("month") String month, @RequestParam("day") String day,   @RequestParam("cycle") String cycle){
        JSONObject json = new JSONObject();
        Long statusReal = 200L;
        try {
            executeService.HMCreateReport(demandId, schemaCode, year, month, day, cycle);
            json.put("message","手动生成周报成功");
        }catch (PortalException e) {
            statusReal = 500L;
            json.put("message", "手动生成周报失败：" + e.getErrMsg());
        }
        return ResponseResult.<JSONObject>builder().errcode(statusReal).data(json).build();
    }

    @ApiOperation(value = "手动生成招聘周报复核")
    @PostMapping("/HMCreateReportMgr")
    public ResponseResult<?> HMCreateReportMgr(@RequestParam("demandId") String demandId){
        JSONObject json = new JSONObject();
        Long statusReal = 200L;
        try {
            executeService.HMCreateReportMgr(demandId);
            json.put("message","手动生成招聘周报复核成功");
        }catch (PortalException e) {
            statusReal = 500L;
            json.put("message", "手动生成招聘周报复核失败：" + e.getErrMsg());
        }
        return ResponseResult.<JSONObject>builder().errcode(statusReal).data(json).build();
    }

}
